using AutoMapper;
using Process.Domain.DTO.InputDTO;
using Process.Domain.DTO.OutDTO;
using Process.Domain.Entities;
using Process.Infrastructure.Repositories;
using Process.Result;
using SqlSugar;
using System;
using System.Collections.Generic;

namespace Process.Application.Department
{
    public class DepartmentService : IDepartmentService
    {
        private readonly IProcessRepository<DepartmentModel> _db;
        private readonly IMapper _mapper;

        public DepartmentService(IProcessRepository<DepartmentModel> db, IMapper mapper)
        {
            _db = db;
            _mapper = mapper;
        }

        public ApiResult<List<PagingDepartmentDTO>> GetAllDepartments()
        {
            var departments = _db.GetValue().Where(x => x.IsDeleted == false).OrderByDescending(x => x.CreatedAt);
               
                

            var result = _mapper.Map<List<PagingDepartmentDTO>>(departments);

            return new ApiResult<List<PagingDepartmentDTO>>
            {
                Success = ResultCode.Success,
                Data = result,
                Message = "获取部门列表成功"
            };
        }

        public ApiResult<PagingDepartmentDTO> GetDepartmentById(long id)
        {
            var department = _db.GetModel(id)
               ;

            if (department == null)
            {
                return new ApiResult<PagingDepartmentDTO>
                {
                    Success = ResultCode.Error,
                    Message = "未找到对应部门信息",
                    Data = null
                };
            }

            var result = _mapper.Map<PagingDepartmentDTO>(department);

            return new ApiResult<PagingDepartmentDTO>
            {
                Success = ResultCode.Success,
                Data = result,
                Message = "获取部门信息成功"
            };
        }

        public ApiResult<ShowDepartmnetDTO> GetDepartmentById2(long id)
        {
            var department = _db.GetModel(id)
               ;

            if (department == null)
            {
                return new ApiResult<ShowDepartmnetDTO>
                {
                    Success = ResultCode.Error,
                    Message = "未找到对应部门信息",
                    Data = null
                };
            }

            var result = _mapper.Map<ShowDepartmnetDTO>(department);

            return new ApiResult<ShowDepartmnetDTO>
            {
                Success = ResultCode.Success,
                Data = result,
                Message = "获取部门信息成功"
            };
        }

        public Apipaging<PagingDepartmentDTO> PagingDepartment(PagingDepartmentInput input)
        {
            var query = _db.GetValue().Where(x => x.IsDeleted == false).OrderByDescending(x => x.CreatedAt);

            

            var total = 0;
            var result = query.OrderBy(d => d.CreatedAt);

            var departments = _mapper.Map<List<PagingDepartmentDTO>>(result);

            return new Apipaging<PagingDepartmentDTO>
            {
                TotalCount = input.PageIndex,
                TotalPages = input.PageSize,              
                PagingData = departments
            };
        }

        public ApiResult<List<ShowDepartmnetDTO>> ShowDepartments()
        {
            var query = _db.GetValue().Where(x => x.IsDeleted == false).OrderByDescending(x => x.CreatedAt);



            var total = 0;
            var result = query.OrderBy(d => d.CreatedAt);

            var departments = _mapper.Map<List<ShowDepartmnetDTO>>(result);

            return new ApiResult<List<ShowDepartmnetDTO>>
            {
                Success = ResultCode.Success,
                Data = departments,
                Message = "获取部门信息成功"
            };
        }
    }
} 